package com.lgkj.ui.shop;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.adapter.OrderCarAdapter;
import com.lgkj.bean.meitao.ComfirOrderBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.ui.main.CarFragment;
import com.lgkj.ui.userinfo.AdressActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class ComfirOrderActivity extends BaseActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_adress)
    TextView tvAdress;
    @BindView(R.id.tv_freight_price)
    TextView tvFreightPrice;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.lv_goods)
    ListView lvGoods;
    private List<ComfirOrderBean> cargoosbea;
    private String adressId = "";
    private String orderInfo = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_comfir_order;
    }

    @Override
    public void initView() {
        setTitle("确认订单");
    }


    @Override
    public void initData() {
        CarFragment.isResu = false;
        cargoosbea = ((List<ComfirOrderBean>) SharedPreferencesUtils.getBean(ComfirOrderActivity.this, "carinfo"));
        if (cargoosbea.size() == 0) {
            return;
        }

        OrderCarAdapter adapter = new OrderCarAdapter(ComfirOrderActivity.this);
        adapter.setCarGoodsBean(cargoosbea);
        lvGoods.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        BigDecimal total = cargoosbea.get(0).getTotalPrice();
//        if (total == null) {
//            total = "1";
//        }
        String totals = getIntent().getStringExtra("total");
        if (totals != null) {
            tvTotalPrice.setText(totals);
        } else {
            BigDecimal totalPrice = total.add(new BigDecimal("18"));
            Log.i(TAG, "initData: " + cargoosbea.get(0).OrderInfo() + "----" + total + "," + totalPrice);
            tvTotalPrice.setText(total.setScale(2, RoundingMode.HALF_UP) + "");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAdress();
        adressId = "";
        orderInfo = "";
    }

    private void getAdress() {
        showDiaLog("正在获取默认地址");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        HttpUtils.postHttp(map, CommonConstant.getDefaultAddress, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    initAdress(jsonObject.optJSONObject("data"));
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    private void initAdress(JSONObject object) {
        if (object == null) {
            toastLong("暂无收获地址，请先添加地址", 0);
            return;
        }
        tvName.setText(object.optString("name"));
        tvPhone.setText(object.optString("phone"));
        tvAdress.setText(object.optString("province_name") + object.optString("city_name") + object.optString("region_name") + "  " + object.optString("address"));
        adressId = object.optString("id");
    }

    @OnClick({R.id.iv_btn_comfir, R.id.lv_adress})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lv_adress:
                startsActivity(new Intent(ComfirOrderActivity.this, AdressActivity.class));
                break;
            case R.id.iv_btn_comfir:
                getTotal();
                break;
        }
    }


    private void getTotal() {
        for (int i = 0; i < cargoosbea.size(); i++) {
            Log.i(TAG, "initData: " + cargoosbea.get(i).OrderInfo());
            orderInfo = orderInfo + cargoosbea.get(i).OrderInfo();
        }
        Log.i(TAG, "initData: " + orderInfo);
        showDiaLog("正在提交");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("address_id", adressId);
        map.put("order_info", orderInfo);
        HttpUtils.postHttp(map, CommonConstant.prePayOrder, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    Intent intent = new Intent(ComfirOrderActivity.this, PayOrderActivity.class);
                    String total_price = jsonObject.optJSONObject("data").optString("total_price");
                    String order_no = jsonObject.optJSONObject("data").optString("order_no");
                    intent.putExtra("order_no", order_no);
                    intent.putExtra("tvTotalPrice", total_price);
                    startsActivity(intent);
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }


}
